import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../vue-components/Login.vue'
import Home from '../vue-components/Home.vue'
import Welcome from '../vue-components/Welcome.vue'
import Users from '../vue-components/user/Users.vue'
import Roles from '../vue-components/powers/Roles.vue'
import Rights from '../vue-components/powers/Rights.vue'
import Categories from '../vue-components/goods/Categories.vue'
import Params from '../vue-components/goods/Params.vue'
import List from '../vue-components/goods/List.vue'
import AddGoods from '../vue-components/goods/AddGoods.vue'
import Order from '../vue-components/order/Order.vue'
import Report from '../vue-components/report/Report.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home, redirect: '/welcome', children: [{ path: '/welcome', component: Welcome }, { path: '/users', component: Users }, { path: '/roles', component: Roles }, { path: '/rights', component: Rights }, { path: '/categories', component: Categories }, { path: '/params', component: Params }, { path: '/goods', component: List }, { path: '/goods/add', component: AddGoods }, { path: '/goods/edit', component: AddGoods, name: 'Edit' }, { path: '/orders', component: Order }, { path: '/reports', component: Report }] }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
